import { Controller } from "../../mvc/core/Controller";
import { IReliveController } from "./interfaces/IReliveController";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
import { RevivalPropWindow } from "../view/revival/RevivalPropWindow";
import { IReliveModule } from "../../../modules/interfaces/IReliveModule";
import { DataEvent } from "../events/DataEvent";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { GameProxy } from "../mvc/GameProxy";
import { ERevivalType } from "../../../Message/Game/ERevivalType";
import { EPrictUnit } from "../../../Message/Public/EPrictUnit";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { Cache } from "../cache/Cache";
import { IView } from "../../mvc/interfaces/IView";
import { ReliveModule } from "../../../modules/ReliveModule";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SEntityKillerInfo } from "../../../Message/Public/SEntityKillerInfo";
import { EProp } from "../../../Message/Public/EProp";
import { AutoFight } from "../view/autoFight/AutoFight";
import { SoundManager } from "../../common/sound/SoundManager";
import { SoundTypeConst } from "../../common/sound/SoundTypeConst";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { CopyCodeRule } from "../rules/CopyCodeRule";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { ECopyType } from "../../../Message/Public/ECopyType";
import { Alert } from "../../../com/mui/controls/Alert";
import { TimerAlertWin } from "../view/common/alertwins/TimerAlertWin";
import { GameDefConfig } from "../resource/GameDefConfig";
import { ShopType } from "../resource/info/ShopType";
import { ShopConfig } from "../resource/ShopConfig";
import { ECategory } from "../../../Message/Public/ECategory";
import { EBind } from "../../../Message/Public/EBind";
import { Game } from "../Game";
import { KeyBoardManager } from "../../../com/gengine/keyBoard/KeyBoardManager";
import { MapFileUtil } from "../scene/map/MapFileUtil";
import { EMapRestrictionType } from "../../../Message/Public/EMapRestrictionType";
type int = number;
//class ReliveController
    
    export  class ReliveController extends Controller implements IReliveController
    {
       

        /* internal  */startTimer(): void
        {
            if (this._reliveTimer == null) 
            {
                this._reliveTimer = new SecTimer(3);
                this._reliveTimer.addListener(TimerType.ENTERFRAME, this.onTimerEnterframeHandler);
            }
            this._reliveTimer.start();
            return;
        }

        public get revivalPropWindow(): RevivalPropWindow
        {
            if (this._revivalPropWindow == null) 
            {
                this._revivalPropWindow = new RevivalPropWindow();
            }
            return this._revivalPropWindow;
        }

        public stageResize(): void
        {
            if (_view && !_view.isHide) 
            {
                (_view as IReliveModule).stageResize();
            }
            return;
        }

        /* internal  */onCoinBindRelive(arg1: DataEvent): void
        {
            if (RolePlayer.instance.isDead) 
            {
                GameProxy.sceneProxy.revival(ERevivalType.convert(ERevivalType._ERevivalTypeInSitu), EPrictUnit.convert(EPrictUnit._EPriceUnitGoldBind));
            }
            else 
            {
                this.hideModule();
            }
            return;
        }

        /* internal  */onCoinRelive(arg1: DataEvent): void
        {
            if (RolePlayer.instance.isDead) 
            {
                GameProxy.sceneProxy.revival(ERevivalType.convert(ERevivalType._ERevivalTypeInSitu), EPrictUnit.convert(EPrictUnit._EPriceUnitGold));
            }
            else 
            {
                this.hideModule();
            }
            return;
        }

        /* internal  */onLocalRelive(arg1: DataEvent=null): void
        {
            if (RolePlayer.instance.isDead) 
            {
                GameProxy.sceneProxy.revival(ERevivalType.convert(ERevivalType._ERevivalTypeInSitu), EPrictUnit.convert(EPrictUnit._EPriceUnitGold));
            }
            else 
            {
                this.hideModule();
            }
            return;
        }

        /* internal  */onPropLocal(arg1: DataEvent=null): void
        {
            if (RolePlayer.instance.isDead) 
            {
                if (GameMapUtil.curMapState.isCrossGuildWar) 
                {
                    GameProxy.sceneProxy.revival(ERevivalType.convert(ERevivalType._ERevivalTypeInBackToTheCity), EPrictUnit.convert(EPrictUnit._EPriceUnitGold));
                    return;
                }
                GameProxy.sceneProxy.revival(ERevivalType.convert(ERevivalType._ERevivalTypeInSitu), EPrictUnit.convert(EPrictUnit._EPriceUnitGold));
            }
            else 
            {
                this.hideModule();
            }
            return;
        }

        /* internal  */onCityRelive(arg1: DataEvent=null): void
        {
            if (RolePlayer.instance.isDead) 
            {
                if (cache.nationalTrea.reliveActive()) 
                {
                    GameProxy.nationalTreasure.relive();
                }
                else if (cache.copy.isInRobFlagCopy() || cache.copy.isIn80DefendCopy()) 
                {
                    GameProxy.copy.moveToPassPoint(0);
                }
                else 
                {
                    GameProxy.sceneProxy.revival(ERevivalType.convert(ERevivalType._ERevivalTypeInBackToTheCity), EPrictUnit.convert(EPrictUnit._EPriceUnitGold));
                }
            }
            else 
            {
                this.hideModule();
            }
            return;
        }

        /* internal  */onAdd(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=Cache.instance.role.entityInfo.level;
            view.update(loc1, this._sEntityKillerInfo);
            return;
        }

        /* internal  */stopTimer(): void
        {
            if (this._reliveTimer) 
            {
                this._reliveTimer.stop();
                this._reliveTimer.dispose(false);
                this._reliveTimer = null;
            }
            return;
        }

        protected /* override */ initView(): IView
        {
            this._module = new ReliveModule();
            return this._module;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            RolePlayer.instance.addEventListener(PlayerEvent.ENTITY_Relived, this.onRoleRelived, false, 0, true);
            NetDispatcher.addCmdListener(ServerCommand.Copy_Fail, this.onCopyFailHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateKillByEntity, this.onRoleKillByEntity);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateRevivalPropNotEnough, this.onRevivalPropNotEnoughHandler);
            Dispatcher.addEventListener(EventName.Role_Relive_CancelBuyProp, this.onCancelBuyPropHandler);
            Dispatcher.addEventListener(EventName.Role_Relive_SureBuyProp, this.onSureBuyPropHandler);
            Dispatcher.addEventListener(EventName.Role_Relive_City, this.onCityRelive);
            Dispatcher.addEventListener(EventName.Role_Relive_Local, this.onLocalRelive);
            Dispatcher.addEventListener(EventName.Role_Relive_PropLocal, this.onPropLocal);
            Dispatcher.addEventListener(EventName.Role_Relive_HideRelivePanel, this.hideRelivePanel);
            Dispatcher.addEventListener(EventName.CDMapTimerComplete, this.onCDMapTimerComplete);
            return;
        }

        /* internal  */onCDMapTimerComplete(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityKillerInfo;
            var loc2=Cache.instance.pack.backPackCache.getPropByType(EProp._EPropRevival).length;
            if (AutoFight.currentAutoFight.isAutoFight && AutoFight.currentAutoFight.locRevive && loc2 > 0) 
            {
                this.onPropLocal();
            }
            else if (AutoFight.currentAutoFight.isAutoFight && AutoFight.currentAutoFight.locRevive && this.isMoneyEnough()) 
            {
                if ((view as IReliveModule).isAutoBuy) 
                {
                    this.onPropLocal();
                }
                else 
                {
                    this.createNewInfo(loc1);
                }
            }
            else 
            {
                this.createNewInfo(loc1);
            }
            return;
        }

        /* internal  */createNewInfo(arg1: SEntityKillerInfo): void
        {
            var loc1=null;
            if (arg1) 
            {
                if (loc1 == null) 
                {
                    loc1 = new SEntityKillerInfo();
                }
                loc1.entityId = this._sEntityKillerInfo.entityId;
                loc1.camp = this._sEntityKillerInfo.camp;
                loc1.level = this._sEntityKillerInfo.level;
                loc1.name = this._sEntityKillerInfo.name;
                loc1.force = this._sEntityKillerInfo.force;
                loc1.waitSec = 0;
                if (view && !view.isHide) 
                {
                    view.update(cache.role.entityInfo.level, loc1);
                }
            }
            return;
        }

        /* internal  */onRoleRelived(arg1: PlayerEvent): void
        {
            SoundManager.instance.soundPlay(SoundTypeConst.Resurrection);
            this.hideRelivePanel();
            return;
        }

        /* internal  */hideRelivePanel(arg1: DataEvent=null): void
        {
            this.hideModule();
            this.revivalPropWindow.hide();
            if (AutoFight.currentAutoFight.isAutoFight) 
            {
                AIManager.onAIControl(null, AIType.AI_AutoFight);
            }
            return;
        }

        /* internal  */onRoleDead(arg1: PlayerEvent): void
        {
            this.roleDead();
            RolePlayer.instance.removeEventListener(PlayerEvent.ENTITY_DEAD, this.onRoleDead, false);
            return;
        }

        /* internal  */onTimerEnterframeHandler(arg1: SecTimer): void
        {
            if (RolePlayer.instance.isDead == false) 
            {
                this.stopTimer();
                return;
            }
            if (RolePlayer.instance.isDead && this.needShowRelivePanel()) 
            {
                if (!_view || _view.isHide) 
                {
                    this.onCityRelive(null);
                    return;
                }
            }
            return;
        }

        /* internal  */onRoleKillByEntity(arg1: MessageBlock): void
        {
            this._sEntityKillerInfo = arg1.messageBase as SEntityKillerInfo;
            this.roleDead();
            return;
        }

        /* internal  */isMoneyEnough(): Boolean
        {
            var loc1=false;
            var loc2=Cache.instance.role.money.gold;
            var loc3=Cache.instance.role.money.goldBind;
            if (loc2 >= 10 || loc3 >= 10) 
            {
                loc1 = true;
            }
            else 
            {
                loc1 = false;
            }
            return loc1;
        }

        /* internal  */checkCDMap(arg1: SEntityKillerInfo): Boolean
        {
            var loc1=0;
            if (arg1 && arg1.waitSec == 0 && this.isCanReliveByProp()) 
            {
                loc1 = Cache.instance.pack.backPackCache.getPropByType(EProp._EPropRevival).length;
                if (AutoFight.currentAutoFight.isAutoFight && AutoFight.currentAutoFight.locRevive && loc1 > 0) 
                {
                    this.onPropLocal();
                    return true;
                }
                if (AutoFight.currentAutoFight.isAutoFight && AutoFight.currentAutoFight.locRevive && this.isMoneyEnough() && (view as IReliveModule).isAutoBuy) 
                {
                    this.buyAddUse(1);
                    return true;
                }
            }
            return false;
        }

        /* internal  */isCanReliveByProp(): Boolean
        {
            if (cache.copy.isInCopy && !cache.copy.checkIsCanReliveByProp()) 
            {
                return false;
            }
            return true;
        }

        /* internal  */roleDead(): void
        {
            var loc2=0;
            if (!this.needShowRelivePanel()) 
            {
                return;
            }
            this.startTimer();
            var loc1=false;
            if (GameMapUtil.isSpecialMap()) 
            {
                loc1 = this.checkCDMap(this._sEntityKillerInfo);
            }
            else if (AutoFight.currentAutoFight.isAutoFight) 
            {
                if (AutoFight.currentAutoFight.locRevive) 
                {
                    loc2 = Cache.instance.pack.backPackCache.getPropByType(EProp._EPropRevival).length;
                    if (GameMapUtil.curMapState.isVIPHook || !Cache.instance.copy.isInCopy || Cache.instance.copy.isInMagicTower() && !(Cache.instance.copy.copyInfo.code == CopyCodeRule.Copy90Tower)) 
                    {
                        if (loc2 > 0) 
                        {
                            GameProxy.sceneProxy.revival(ERevivalType.convert(ERevivalType._ERevivalTypeInSitu), EPrictUnit.convert(EPrictUnit._EPriceUnitGold));
                            MsgManager.showRollTipsMsg(Language.getString(20800));
                            return;
                        }
                        if (cache.login.loginGame.money.gold >= 10) 
                        {
                            this.buyAddUse(1);
                            MsgManager.showRollTipsMsg(Language.getString(20800));
                            return;
                        }
                    }
                    else if (this.checkCanAutoLocRevie()) 
                    {
                        if (loc2 > 0) 
                        {
                            GameProxy.sceneProxy.revival(ERevivalType.convert(ERevivalType._ERevivalTypeInSitu), EPrictUnit.convert(EPrictUnit._EPriceUnitGold));
                            MsgManager.showRollTipsMsg(Language.getString(20800));
                            return;
                        }
                    }
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoFightEnd));
            }
            if (!loc1) 
            {
                this.showModule();
                this.onAdd();
            }
            return;
        }

        public checkCanAutoLocRevie(): Boolean
        {
            if (GameMapUtil.curMapState.isCrossBeach) 
            {
                return true;
            }
            if (GameMapUtil.curMapState.isGuildAltar) 
            {
                return true;
            }
            if (cache.copy.isInCopyType(ECopyType._ECopyGuildDefense)) 
            {
                return true;
            }
            if (Cache.instance.copy.isInboundlessSeaCopy()) 
            {
                return true;
            }
            if (GameMapUtil.curMapState.isGuildMap) 
            {
                return true;
            }
            if (GameMapUtil.curMapState.isGuildPasture) 
            {
                return true;
            }
            if (GameMapUtil.curMapState.isGuildWar) 
            {
                return true;
            }
            if (cache.copy.isInCopyType(ECopyType._ECopyCrossStair)) 
            {
                return true;
            }
            if (cache.copy.isInCopyType(ECopyType._ECopyPeaceField)) 
            {
                return true;
            }
            return false;
        }

        /* internal  */onCopyFailHandler(arg1: DataEvent): void
        {
            Alert.mode = Alert.Mode_NoneNotModal;
            Alert.alertWinRenderer = TimerAlertWin;
            Alert.timerOut = 10;
            Alert.buttonWidth = 60;
            var loc1="";
            if (cache.copy.isIn80DefendCopy()) 
            {
                loc1 = Language.getString(20821);
            }
            else 
            {
                loc1 = Language.getString(20801);
            }
            Alert.show(loc1, null, Alert.OK, null, this.onAlertOkHandler);
            return;
        }

        /* internal  */onAlertOkHandler(arg1: int): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveWithoutComfirm));
            return;
        }

        /* internal  */onRevivalPropNotEnoughHandler(arg1: MessageBlock): void
        {
            if ((view as IReliveModule).isAutoBuy) 
            {
                this.buyAddUse(1);
            }
            else 
            {
                this.revivalPropWindow.show();
            }
            return;
        }

        /* internal  */onCancelBuyPropHandler(arg1: DataEvent): void
        {
            this.revivalPropWindow.hide();
            this.showModule();
            return;
        }

        /* internal  */onSureBuyPropHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.buyAddUse(loc1);
            return;
        }

        /* internal  */buyAddUse(arg1: int): void
        {
            var loc2=0;
            var loc4=null;
            var loc1=GameDefConfig.instance.getEShopCode(ShopType.BindShop);
            var loc3=ShopConfig.instance.getShopItemInfoArrayByType(ECategory._ECategoryProp, EProp._EPropRevival);
            var loc5=0;
            var loc6=loc3;
            for(loc4 of loc6) 
            {
                if (!(!(loc4 == null) && loc4.itemInfo.item.bind == EBind._EBindYes)) 
                {
                    continue;
                }
                loc2 = loc4.itemInfo.item.code;
                break;
            }
            GameProxy.shopProxy.buyItemAndUse(0, loc1, loc2, arg1, 1);
            return;
        }

        /* internal  */hideModule(): void
        {
            view.hide();
            if (Game.scene.mouseEnabled == false) 
            {
                KeyBoardManager.instance.addListener();
                Game.scene.mouseEnabled = true;
            }
            return;
        }

        /* internal  */showModule(): void
        {
            view.show();
            if (Game.scene.mouseEnabled) 
            {
                KeyBoardManager.instance.cancelListener();
                Game.scene.mouseEnabled = false;
            }
            return;
        }

        /* internal  */needShowRelivePanel(): Boolean
        {
            if (Game.sceneInfo.isCanRevival == false) 
            {
                return false;
            }
            if (Cache.instance.copy.isInLv70DramaCopy()) 
            {
                return false;
            }
            if (Cache.instance.copy.isInEatBeansCopy()) 
            {
                return false;
            }
            if (GameMapUtil.isArena()) 
            {
                if (!cache.arena.arenaGroup || cache.arena.arenaGroup.members.length == 0) 
                {
                    return false;
                }
            }
            if (GameMapUtil.curMapState.isWrestle) 
            {
                return false;
            }
            if (GameMapUtil.curMapState.isArenaCross) 
            {
                return false;
            }
            if (GameMapUtil.curMapState.isGangFights && cache.gangFights.isResult) 
            {
                return false;
            }
            if (GameMapUtil.curMapState.isLadderBattlefield) 
            {
                return false;
            }
            var loc1=GameMapUtil.isCopyMap(MapFileUtil.mapID);
            if (loc1) 
            {
                if (Game.sceneInfo.sMapDefine.restrictionType & EMapRestrictionType._EMapRestrictionTypeRevivalInSitu && Game.sceneInfo.sMapDefine.restrictionType & EMapRestrictionType._EMapRestrictionTypeRevivalInBackToTheCity) 
                {
                    return false;
                }
            }
            return true;
        }

        private /* var */_revivalPropWindow: RevivalPropWindow;

        private /* var */_module: IReliveModule;

        private /* var */_sEntityKillerInfo: SEntityKillerInfo;

        private /* var */_reliveTimer: SecTimer;
    }
